package cn.imu.service.impl;

import cn.imu.entity.User;
import cn.imu.mapper.UserMapper;
import cn.imu.service.UserService;
import org.apache.ibatis.transaction.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private User user;
    @Autowired
    DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    TransactionDefinition transactionDefinition;


    @Override
    public User selectUserByUsername(String username) {
        user.setUsername(username);
        return userMapper.selectUserByUsername(user);
    }

   /* @Override
    public int insertUser(User user) {
        return 0;
    }*/


    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public int insertUser(User user) {

       int result= userMapper.insertUser(user);
        //验证发生异常时，事务的回滚
        if(true){
            throw new RuntimeException("error occurs");
        }
        return  result;
    }
}
